package cn.com.ddapp.ddm.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.com.ddapp.ddm.AppConfig;
import cn.com.ddapp.ddm.R;
import cn.com.ddapp.ddm.adapter.GoodsLandAdapter;
import cn.com.ddapp.ddm.api.OKUtilAPI;
import cn.com.ddapp.ddm.base.BaseActivity;
import cn.com.ddapp.ddm.base.BaseApplication;
import cn.com.ddapp.ddm.bean.HomeBean;
import cn.com.ddapp.ddm.inter.OkUtilCallback;
import cn.com.ddapp.ddm.ui.empty.EmptyLayout;
import cn.com.ddapp.ddm.util.NetUtils;
import cn.com.ddapp.ddm.util.SPUtils;
import cn.com.ddapp.ddm.util.TLog;

/**
 * Created by Administrator on 2016/8/31 0031.//横屏的更多
 */
public class CommidGoodsActivity extends BaseActivity {
    @BindView(R.id.fenList)
    ListView fenList;
    @BindView(R.id.error_layout)
    EmptyLayout mEmptyLayout;
    private HomeBean homeBean;


    @Override
    public void initView() {

    }

    @Override
    public void initData() {
        if (!NetUtils.isConnected(this)) {
            Toast.makeText(this, "请检查你的网络", Toast.LENGTH_SHORT).show();
        } else {
            mEmptyLayout.setErrorType(EmptyLayout.NETWORK_LOADING);
            OkUtilCallback okUtilCallback = new OkUtilCallback(mEmptyLayout) {
                @Override
                public void onMyResponse(String response, int id) {
                    TLog.log(getFragmentTag(), response);
//                    Log.e("TAG", "onMyResponse: "+response );
                    homeBean = gson.fromJson(response, HomeBean.class);
                    if ((!AppConfig.RESULT_OK.equalsIgnoreCase(homeBean.getResult())) && homeBean.getLogin()) {
                        mEmptyLayout.setErrorType(EmptyLayout.NO_LOGIN);
                    } else if (homeBean.getResult().equals("OK") && homeBean.getSortList().size() == 0) {
                        mEmptyLayout.setErrorType(EmptyLayout.NODATA);
                    } else if ((!AppConfig.RESULT_OK.equalsIgnoreCase(homeBean.getResult()))) {
                        BaseApplication.getIntstance().showToast(homeBean.getResult(), Toast.LENGTH_SHORT, 0, Gravity.TOP);
                    } else if ((AppConfig.RESULT_OK.equalsIgnoreCase(homeBean.getResult()))) {
                         initLand();
                    }
                }
            };
            OKUtilAPI.get(OKUtilAPI.homeurl + "?addrId=" + AppConfig.CITY_ID, null, okUtilCallback);
        }


    }

    private void initLand() {
        fenList.setAdapter(new GoodsLandAdapter(this, homeBean.getSortList()));
        fenList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                Intent intent = new Intent(CommidGoodsActivity.this, GoodClassifyActivity.class);
                String lId=homeBean.getSortList().get(position).getLevelId();
                SPUtils.setStringData(CommidGoodsActivity.this,SPUtils.LID,lId);
                intent.putExtra("classiftId",homeBean.getSortList().get(position).getLevelId());
                startActivity(intent);
            }
        });
    }

    @Override
    public int getContentViewId() {
        return R.layout.activity_commid_good;
    }

    @Override
    protected String getFragmentTag() {
        return null;
    }


}
